package com.example.hy.androidthreadtest;

import android.app.ActionBar;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

 class DownloadTask extends AsyncTask<Void,Integer,Boolean> {

    private ActionBar progressDialog;
    private Context context;

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        try {
            while (true){
                int downloadPercent = doDownload(); //虚构的方法
                publishProgress(downloadPercent);
                if (downloadPercent >= 100){
                    break;
                }
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        //更新下载速度
        progressDialog.setMessage("Downloaded" + values[0] + "%");

    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        progressDialog.dismiss();

//下载结果
        if (result){
            Toast.makeText(context,"Download succrrdrd",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(context,"Download failed",Toast.LENGTH_SHORT).show();
        }

       new DownloadTask().execute();
    }


}
